import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;


public class CatalogPanel extends JPanel{
	JPanel panel;
	
	public CatalogPanel(){
		panel=new JPanel();
		panel.setPreferredSize(new Dimension(600,400));
		//panel.setLayout(new BorderLayout());
		panel.setBackground(new Color(169,25,25));
		setBackground(new Color(169,25,25));
		
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		JScrollPane scroll=new JScrollPane(panel);//,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroll.setMinimumSize(new Dimension(600,400));
		scroll.setPreferredSize(new Dimension(600,400));
		
		initialize();
		
		add(scroll);
		setSize(500,100);
		setVisible(true);
	}
	
	private void newCourse(String courseName,int ch, String courseDescription, String prerequisite){
		//course panel
		JPanel course1=new JPanel();
		course1.setPreferredSize(new Dimension(550,80));
		course1.setLayout(new BorderLayout());
		course1.setBackground(Color.white);
		course1.setBorder(BorderFactory.createLineBorder((Color.black)));
		
		//name
		JPanel namepanel=new JPanel();
		namepanel.setBorder(BorderFactory.createLineBorder((Color.black)));
		JLabel name=new JLabel(courseName+ "  "+ch+"credits");
		//name.setBackground(Color.white);
		//namepanel.setBackground(new Color(169,25,25));
		namepanel.add(name);
		
		
		course1.add(namepanel,BorderLayout.NORTH);
		
		//description
		JPanel descriptionpanel=new JPanel();
		descriptionpanel.setBackground(Color.white);
		descriptionpanel.setLayout(new BorderLayout());
		JLabel description=new JLabel(courseDescription);
		
		descriptionpanel.add(description,BorderLayout.NORTH);
		
		//requisites
		JPanel requisitepanel=new JPanel();
		requisitepanel.setBackground(Color.white);
		requisitepanel.setLayout(new GridLayout(1,2));
		JLabel requisite=new JLabel("Prerequisites: ");
		JLabel courseRequisite=new JLabel(prerequisite);
		requisitepanel.add(requisite);
		requisitepanel.add(courseRequisite);
		
		descriptionpanel.add(requisitepanel,BorderLayout.CENTER);
		
		course1.add(descriptionpanel,BorderLayout.CENTER);
		

		JPanel empty=new JPanel();
		empty.setPreferredSize(new Dimension(600,5));
		empty.setBackground(new Color(169,25,25));
		course1.add(empty,BorderLayout.SOUTH);
		
		panel.add(course1);
	}
	
	private void initialize(){
		//load student's major and display courses accordingly!!!
		
		//load data from the database
		//and call newCourse, count the num of courses required using SQL count*
		String name,description,prerequisite;
		int credits, numCourses=3;
		//for(int i=0;i<numCourses;i++){
		//get the course info from database=course
		
		//repeat it in for loop!
		
		int i=0;
		
		try{
			String dName = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
			Connection con = DriverManager.getConnection(url);
			java.sql.Statement mysqlStatement = con.createStatement();
			java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, description, prerequisite, credit_hours, course_number FROM course_catalog WHERE major IN (SELECT major FROM student_list WHERE SID='"+MenuPanel.SID+"')");
			
			while (resultSet.next()) {
				i++;
				name=resultSet.getString(5)+" - "+resultSet.getString(1);
				description=resultSet.getString(2);
				prerequisite=resultSet.getString(3);
				credits=resultSet.getInt(4);
				newCourse(name,credits,description,prerequisite);
			}
			con.close();
			}
			catch(Exception ex){}
		
		//if numCourses>3, adjust panel size to accommodate more course panels
		numCourses=i;
		int y=400;
		if(i>3)
			y+=(numCourses-3)*133;
		panel.setPreferredSize(new Dimension(600,y));
		
	}
}
